<?php
/* ************************************************** *\
 * @company		: NovaITech							  *	
 * @author		: van.vo@novaitech.com				  * 
 * @copyright	: 2012								  * 
 * ================================================== *
 * @date		: {creation_date}
 * @project		: 
 * @description	: {description}
 *		- 
 *
\* ************************************************** */
class Promotion extends MY_Controller {
    
    function __construct( ) {
        parent::__construct( );
		$this->load->library( array( 'form_validation', 'table' ) );		
		$this->load->helper( array( 'form', 'url' ) );
		$this->load->model( 'CodeGenerator_model', '', TRUE );
		$this->load_template();
	}	
	
	function index( ){
		$this->manage( );
	}

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: View list
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function view( ){
        // Get rules from config file
        // --------------------------------------------------
	    //$rules                  =   $this->form_validation->_config_rules[ 'promotion_add' ];
        $rules                  =   array(array('field_id' => 'Promo_ID','label' => 'Promo_ID','control' => 'Array',),array('field_id' => 'Eff_Dt','label' => 'Eff_Dt','control' => 'Array',),array('field_id' => 'Exp_Dt','label' => 'Exp_Dt','control' => 'Array',),array('field_id' => 'Create_Dt','label' => 'Create_Dt','control' => 'Array',),array('field_id' => 'Serv_ID1','label' => 'Serv_ID1','control' => 'Array',),array('field_id' => 'Discount_Percent1','label' => 'Discount_Percent1','control' => 'Array',),array('field_id' => 'Serv_ID2','label' => 'Serv_ID2','control' => 'Array',),array('field_id' => 'Discount_Percent2','label' => 'Discount_Percent2','control' => 'Array',),array('field_id' => 'Serv_ID3','label' => 'Serv_ID3','control' => 'Array',),array('field_id' => 'Discount_Percent3','label' => 'Discount_Percent3','control' => 'Array',),);
	    $this->data[ 'rules' ]  =   $rules;
       
        // Make header
        // --------------------------------------------------
	    $header = array( );
	    foreach( $rules as $item ){
	        $header[ ] = $item[ 'label' ];
	    }
       
	    $this->data[ 'header' ] =   $header;
	    $results                =   $this->CodeGenerator_model->get( 'promotion', 'Promo_ID,Eff_Dt,Exp_Dt,Create_Dt,Serv_ID1,Discount_Percent1,Serv_ID2,Discount_Percent2,Serv_ID3,Discount_Percent3' );
	
        $actions  = array();
            
        
        
        // Make action Rent/Contact/Review
        // --------------------------------------------------
        for( $i = 0; $i < count( $results ); $i++ ){
            $id = array_values( $results[ $i ] );
            
            if(!empty($actions)){
                foreach ($actions as $item) {
                    $results[ $i ][ $item['action_name'] ]   = anchor( base_url( ) . 
                    'index.php/promotion/'. $item['action_func'] . "/" . $id[ 0 ], $item['action_name']);
                }
            }
            
        }
        
        $this->data[ 'results'        ]   =   $results;
        $this->data[ 'master_limited' ]   =   explode(',', 'Promo_ID,Eff_Dt,Exp_Dt,Create_Dt,Serv_ID1,Discount_Percent1,Serv_ID2,Discount_Percent2,Serv_ID3,Discount_Percent3');
        $this->data[ 'custom_action'  ]   =   $actions;
 
        $this->load->view( 'promotion_list', $this->data ); 
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Manage list
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function manage( ){
        // Get rules from config file
        // --------------------------------------------------
        //$rules                  =   $this->form_validation->_config_rules[ 'promotion_add' ];
        $rules                  =         array(array('field_id' => 'Promo_ID','label' => 'Khuyến mãi ID','control' => 'Array','class' => 'first border-right', 'style' => 'border-right: solid 3px #fff;'),array('field_id' => 'Eff_Dt','label' => 'Ngày bắt đầu','control' => 'time','class' => 'border-right', 'style' => 'border-right: solid 3px #fff;'),array('field_id' => 'Exp_Dt','label' => 'Ngày hết hạn','control' => 'time','class' => 'border-right', 'style' => 'border-right: solid 3px #fff;'),array('field_id' => 'Create_Dt','label' => 'Ngày tạo','control' => 'time','class' => 'border-right', 'style' => 'border-right: solid 3px #fff;'),array('field_id' => 'Serv_ID1_Name','label' => 'Dịch vu 1','control' => 'Array','class' => 'border-right', 'style' => 'border-right: solid 3px #fff;'),array('field_id' => 'Discount_Percent1','label' => 'Phần trăm','control' => 'Array','class' => 'border-right', 'style' => 'border-right: solid 3px #fff;'),);
        $this->data[ 'rules' ]  =   $rules;
       
        // Make header
        // --------------------------------------------------
        $header = array( );
        foreach( $rules as $item ){
            $header[ ] = $item[ 'label' ];
        }
       
        $this->data[ 'header' ] =   $header;
        $results                =   $this->CodeGenerator_model->get( 'promotion', 'Promo_ID,Eff_Dt,Exp_Dt,Create_Dt,Serv_ID1,Discount_Percent1,Serv_ID2,Discount_Percent2,Serv_ID3,Discount_Percent3' );
        // Make action Edit/Delete
        // --------------------------------------------------
        for( $i = 0; $i < count( $results ); $i++ ){
	       $id = array_values( $results[ $i ]);
            $results[ $i ][ 'Edit'   ]   =  anchor( base_url( ) . 'index.php/promotion/edit/' . $id[ 0 ], 'Edit');  
            $results[ $i ][ 'Delete' ]   =  anchor( 
                        base_url( ) . 'index.php/promotion/delete/' . $id[ 0 ], 
                        'Delete',
                        array( 'onClick' => 'return deletechecked(\' ' . base_url() . 'index.php/promotion/delete/' . $id[ 0 ] . ' \')' ) );    
			$servid1 =  $results[ $i ][ 'Serv_ID1'   ] ;
			$sql = "select s.*, c.Name as Category_Name from services as s, categories as c where s.Cat_ID=c.Ca_ID and s.Serv_ID='$servid1'";
			$serv1   =   $this->CodeGenerator_model->custom_query($sql,"array");
			if(!empty($serv1))
				$results[ $i ][ 'Serv_ID1_Name'   ] = $serv1[0]['Category_Name']."-".$serv1[0]['Serv_Name'];
			else $results[ $i ][ 'Serv_ID1_Name'   ] = "";
			$servid2 =  $results[ $i ][ 'Serv_ID2'   ] ;
			$sql = "select s.*, c.Name as Category_Name from services as s, categories as c where s.Cat_ID=c.Ca_ID and s.Serv_ID='$servid2'";
			$serv2   =   $this->CodeGenerator_model->custom_query($sql,"array");
			if(!empty($serv2))
				$results[ $i ][ 'Serv_ID2_Name'   ] = $serv2[0]['Category_Name']."-".$serv2[0]['Serv_Name'];
			else $results[ $i ][ 'Serv_ID2_Name'   ] = "";
			$servid3 =  $results[ $i ][ 'Serv_ID3'   ] ;
			$sql = "select s.*, c.Name as Category_Name from services as s, categories as c where s.Cat_ID=c.Ca_ID and s.Serv_ID='$servid3'";
			$serv3   =   $this->CodeGenerator_model->custom_query($sql,"array");
			if(!empty($serv3))
				$results[ $i ][ 'Serv_ID3_Name'   ] = $serv3[0]['Category_Name']."-".$serv3[0]['Serv_Name'];
			else $results[ $i ][ 'Serv_ID3_Name'   ] = "";
        }
        
        $this->data[ 'results'        ]     =   $results;
        $this->data[ 'master_limited' ]     =   explode(',', 'Promo_ID,Eff_Dt,Exp_Dt,Create_Dt,Serv_ID1,Serv_ID1_Name,Discount_Percent1,Serv_ID2,Serv_ID2_Name,Discount_Percent2,Serv_ID3,Serv_ID3_Name,Discount_Percent3');
        $this->data[ 'action'         ]     =   array(array('id' => 'new','label' => 'New','url' => 'index.php/promotion/add/',),array('id' => 'edit','label' => 'Edit','url' => 'index.php/promotion/edit/',),array('id' => 'delete','label' => 'Delete','url' => '',),);
        // $this->load->view( 'promotion_list', $this->data ); 
		$this->template->write( 'title', 'Spa - Khuyến mãi' );
        $this->template->write_view( 'content', 'promotion_list.php',$this->data );
        $this->template->render();
    }
	

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Load form add new record
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function add( ){            
		$this->data[ 'custom_error' ] = '';
		$username = "noname";
		$userid = 0;
        $Promo_ID = set_value('Promo_ID');
		if(!$Promo_ID)
			$Promo_ID="Promo_".uniqid();
		$this->data['temp_id'] = $Promo_ID;
		$selected_cat=$this->input->post('Serv_ID1');
		$selected_cat2=$this->input->post('Serv_ID2');
		$selected_cat3=$this->input->post('Serv_ID3');
		$sql = "select s.*, c.Name as Category_Name from services as s, categories as c where s.Cat_ID=c.Ca_ID";
        $categories   =   $this->CodeGenerator_model->custom_query($sql, "array");
		$combobox_cats=array();
		foreach($categories as $cat){
			$combobox_cats['']="";
			$combobox_cats[$cat['Serv_ID']]=$cat['Category_Name']."-".$cat['Serv_Name'];
		}
		$this->data['combobox_cats']=form_dropdown('Serv_ID1', $combobox_cats,$selected_cat);
		$this->data['combobox_cats2']=form_dropdown('Serv_ID2', $combobox_cats,$selected_cat2);
		$this->data['combobox_cats3']=form_dropdown('Serv_ID3', $combobox_cats,$selected_cat3);
        if( $this->form_validation->run( 'promotion_add' ) == false ){
             $this->data[ 'custom_error' ] = ( validation_errors( ) ? '<div class="form_error">' . validation_errors( ) . '</div>' : false );
        }else{                            
            $data = array(
					'Promo_ID' => $Promo_ID,
                    'Eff_Dt' => strtotime(str_replace(array("/"),array("."),set_value('Eff_Dt'))),
					'Exp_Dt' => strtotime(str_replace(array("/"),array("."),set_value('Exp_Dt'))),
					'Create_Dt' => time(),
					'Serv_ID1' => $selected_cat,
					'Discount_Percent1' => set_value('Discount_Percent1'),
					'Serv_ID2' => $selected_cat2,
					'Discount_Percent2' => set_value('Discount_Percent2'),
					'Serv_ID3' => $selected_cat3,
					'Discount_Percent3' => set_value('Discount_Percent3')
            );
            $result = $this->CodeGenerator_model->add( 'promotion', $data, 'Promo_ID' );
			if( $result == ADD_SUCCESSFULLY ){
				redirect( base_url() . 'index.php/promotion/manage/' );
			}else if( $result == RECORD_EXISTED ){
				$this->data[ 'custom_error' ] = '<div class="form_error"><p>A record existed.</p></div>';
			}else{
				$this->data[ 'custom_error' ] = '<div class="form_error"><p>An Error Occured.</p></div>';
			}
		}		   
		// $this->load->view( 'promotion_add', $this->data );   
		$this->template->write( 'title', 'Spa - Thêm khuyến mãi' );
        $this->template->write_view( 'content', 'promotion_add.php',$this->data );
        $this->template->render();
    }	

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Load edit form.
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function edit( ){
        $this->data[ 'custom_error' ] = '';
		$username = "noname";
		$userid = 0;
        $selected_cat=$this->input->post('Serv_ID1');
		$selected_cat2=$this->input->post('Serv_ID2');
		$selected_cat3=$this->input->post('Serv_ID3');
        if( $this->form_validation->run( 'promotion_edit' ) == false ){
            $this->data[ 'custom_error' ] = ( validation_errors( ) ? '<div class="form_error">' . validation_errors( ) . '</div>' : false );

        }else{                            
            $data = array(
                'Eff_Dt' => strtotime(str_replace(array("/"),array("."),$this->input->post('Eff_Dt'))),
					'Exp_Dt' => strtotime(str_replace(array("/"),array("."),$this->input->post('Exp_Dt'))),
					'Create_Dt' => time(),
					'Serv_ID1' => $selected_cat,
					'Discount_Percent1' => $this->input->post('Discount_Percent1'),
					'Serv_ID2' => $selected_cat2,
					'Discount_Percent2' => $this->input->post('Discount_Percent2'),
					'Serv_ID3' => $selected_cat3,
					'Discount_Percent3' => $this->input->post('Discount_Percent3')
            );
			if( $this->CodeGenerator_model->edit( 'promotion', $data, 'Promo_ID', $this->input->post( 'Promo_ID' ) ) == TRUE ){
				redirect( base_url( ) . 'index.php/promotion/manage/' );
			}else{
				$this->data[ 'custom_error' ] = '<div class="form_error"><p>An Error Occured</p></div>';
			}
		}
		$result   =   $this->CodeGenerator_model->get( 'promotion', 'Promo_ID,Eff_Dt,Exp_Dt,Create_Dt,Serv_ID1,Discount_Percent1,Serv_ID2,Discount_Percent2,Serv_ID3,Discount_Percent3', 'Promo_ID = "' . $this->uri->segment( 3 ) . '"', NULL, NULL, true );  
		if(!$selected_cat) $selected_cat = $result->Serv_ID1;
		if(!$selected_cat2) $selected_cat2 = $result->Serv_ID2;
		if(!$selected_cat3) $selected_cat3 = $result->Serv_ID3;
		$sql = "select s.*, c.Name as Category_Name from services as s, categories as c where s.Cat_ID=c.Ca_ID";
        $categories   =   $this->CodeGenerator_model->custom_query($sql, "array");
		$combobox_cats=array();
		foreach($categories as $cat){
			$combobox_cats['']="";
			$combobox_cats[$cat['Serv_ID']]=$cat['Category_Name']."-".$cat['Serv_Name'];
		}
		$this->data['combobox_cats']=form_dropdown('Serv_ID1', $combobox_cats,$selected_cat);
		$this->data['combobox_cats2']=form_dropdown('Serv_ID2', $combobox_cats,$selected_cat2);
		$this->data['combobox_cats3']=form_dropdown('Serv_ID3', $combobox_cats,$selected_cat3);
		$this->data[ 'result' ] = $result;
		// $this->load->view( 'promotion_edit', $this->data );	
		$this->template->write( 'title', 'Spa - Sửa khuyến mãi' );
        $this->template->write_view( 'content', 'promotion_edit.php',$this->data );
        $this->template->render();		
    }

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Delete record
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function delete( ){
        $ID =  $this->uri->segment( 3 );
        $this->CodeGenerator_model->delete(  'promotion', 'Promo_ID', $ID );             
        redirect( base_url( ) . 'index.php/promotion/manage/' );
    }       
}